package com.haojin.java2;

import com.haojin.java1.Person;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author .29.
 * @create 2022-04-19 17:16
 */
public class OtherTest {
    @Test
    public void test1(){
        Class<Person> personClass = Person.class;
        //获取其当前运行时类中声明为public的构造器
        Constructor<?>[] constructors = personClass.getConstructors();
        for (Constructor c :
                constructors) {
            System.out.println(c);
        }
        System.out.println();

        //获取当前运行时类的所有构造器
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor c1 :
                declaredConstructors) {
            System.out.println(c1);
        }
    }
    /*
    获取运行时类的父类

    代码：逻辑性代码 vs 功能性代码
     */

    @Test
    public void test2(){
        Class personClass = Person.class;

        Class superclass = personClass.getSuperclass();
        System.out.println(superclass);

        //获取运行时类带泛型的父类
        Type genericSuperclass = personClass.getGenericSuperclass();
        System.out.println(genericSuperclass);


        //获取运行时类带泛型的父类的泛型
        Type genericSuperclass1 = personClass.getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass1;
        //获取泛型类型
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        System.out.println(((Class)actualTypeArguments[0]).getName());
    }

    /*
    获取运行时类实现的接口
     */
    @Test
    public void test3(){
        Class<Person> personClass = Person.class;

        Class<?>[] interfaces = personClass.getInterfaces();
        for (Class i :
                interfaces) {
            System.out.println(i);
        }

        System.out.println();
        //获取运行时类父类实现的的接口
        Class<?>[] interfaces1 = personClass.getSuperclass().getInterfaces();
        for (Class i :
                interfaces1) {
            System.out.println(i);
        }

    }

    /*
    获取运行时类所在的包
     */
    @Test
    public void test4(){
        Class<Person> personClass = Person.class;

        Package aPackage = personClass.getPackage();
        System.out.println(aPackage);
    }

    /*
    获取运行时类声明的注解
     */
    @Test
    public void test5(){
        Class<Person> personClass = Person.class;

        Annotation[] annotations = personClass.getAnnotations();
        for (Annotation A :
                annotations) {
            System.out.println(A);
        }
    }
}
